<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduDot;
use backend\models\EduUser;

/**
 * EduDotSearch represents the model behind the search form of `backend\models\EduDot`.
 */
class EduDotSearch extends EduDot
{

    public $parent_id;
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'code', 'parent_code', 'gradution_count', 'gradution_base', 'sort', 'rank', 'status', 'create_time', 'collect_count', 'collect_base', 'update_time'], 'integer'],
            [['avatar', 'room_name', 'label_one', 'label_two', 'label_three', 'room_images', 'room_teacher', 'room_address', 'room_des', 'show_image', 'openid', 'parent_id'], 'safe'],
            [['video_fee', 'room_fee', 'total_fee', 'put_money', 'latitude', 'longitude'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduDot::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->joinWith('user');

        // grid filtering conditions
        $query->andFilterWhere([
//            'edu_user.parent_id' => $params['parent_id'],
            'id' => $this->id,
            'user_id' => $this->user_id,
            'code' => $this->code,
            'parent_code' => $this->parent_code,
            'gradution_count' => $this->gradution_count,
            'gradution_base' => $this->gradution_base,
            'video_fee' => $this->video_fee,
            'room_fee' => $this->room_fee,
            'total_fee' => $this->total_fee,
            'put_money' => $this->put_money,
            'sort' => $this->sort,
            'rank' => $this->rank,
            'status' => $this->status,
            'create_time' => $this->create_time,
            'latitude' => $this->latitude,
            'longitude' => $this->longitude,
            'collect_count' => $this->collect_count,
            'collect_base' => $this->collect_base,
            'update_time' => $this->update_time,
        ]);

//        if (array_key_exists('parent_id', $params)) {
//            $query->andFilterWhere([
//                'edu_user.parent_id' => $params['parent_id'],
//            ]);
//        }
        if ($this->parent_id) {
            $query->andFilterWhere([
                'edu_user.parent_id' => $this->parent_id,
            ]);
        }

        $query->andFilterWhere(['like', 'avatar', $this->avatar])
            ->andFilterWhere(['like', 'room_name', $this->room_name])
            ->andFilterWhere(['like', 'label_one', $this->label_one])
            ->andFilterWhere(['like', 'label_two', $this->label_two])
            ->andFilterWhere(['like', 'label_three', $this->label_three])
            ->andFilterWhere(['like', 'room_images', $this->room_images])
            ->andFilterWhere(['like', 'room_teacher', $this->room_teacher])
            ->andFilterWhere(['like', 'room_address', $this->room_address])
            ->andFilterWhere(['like', 'room_des', $this->room_des])
            ->andFilterWhere(['like', 'show_image', $this->show_image])
            ->andFilterWhere(['like', 'openid', $this->openid]);

//        if ($this->parent_id != '') {
//            $query->joinWith([
//                'user' => function(\yii\db\ActiveQuery $query) {
//                    $query->where(['parent_id'=> 10000]);
//                }
//            ]);
//        }

        return $dataProvider;
    }

    /**
     * 跟用户表相连
     * @return \yii\db\ActiveQuery
     *
     */
//    public function getUser()
//    {
//        return $this->hasOne(EduUser::class, ['id' => 'user_id']);
//    }
}
